package lukeer.fencing;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * A board contains many sectors which are generated when a result is calculated. These contain piece objects and
 * provide information for player evaluation functions.
 *
 * User: Luke Richardson lukeer, Wafa M'Barek wmbarek
 * Date: 30/07/12
 * Time: 15:43
 */
class Sector implements csproj.fencing.Piece {
    private final ArrayList<BoardPiece> boardPieces;
    private int black;
    private int white;
    private boolean winnable;
    private boolean eliminated;
    private int result;

    public Sector(ArrayList<BoardPiece> boardPieces) {
        this.boardPieces = boardPieces;
        result = EMPTY;
        if (boardPieces.size() > 8 && boardPieces.size() < 13) winnable = true;
        else if (boardPieces.size() < 9) eliminated = true;
        calculateWinner();
    }

    /**
     * Sets black and white integer values to however many boardPieces the respective players have within the sector
     */
    public void calculateWinner() {
        for (BoardPiece p : boardPieces) {
            if (p.getType() == BLACK) black++;
            if (p.getType() == WHITE) white++;
        }
        if (black > white) result = BLACK;
        if (black < white) result = WHITE;
    }

    /**
     * Returns true if the sector is a valid winning sector
     *
     * @return true if board contains 9-12 boardPieces
     */
    public boolean isWinnable() {
        return winnable;
    }

    /**
     * Returns true if the sector has less than 9 boardPieces, which means that it is eliminated from the board
     *
     * @return true if eliminated from game
     */
    public boolean isEliminated() {
        return eliminated;
    }

    /**
     * Returns number of black boardPieces on board
     *
     * @return number of black pieces
     */
    public int getBlack() {
        return black;
    }

    /**
     * Returns number of white boardPieces on board
     *
     * @return number of white pieces
     */
    public int getWhite() {
        return white;
    }

    /**
     * Returns a int value representing the winner of this sector.
     *
     * @return winner as int
     */
    public int getWinner() {
        return result;
    }

    /**
     * Return true if parameter p is in this sector
     *
     * @param p object used for search
     * @return true if is in sector
     */
    public boolean contains(BoardPiece p) {
        return boardPieces.contains(p);
    }

    /**
     * Returns iterator object for ArrayList of boardPieces
     *
     * @return iterator object
     */
    public Iterator<BoardPiece> iterator() {
        return boardPieces.iterator();
    }

    /**
     * Getter for boardPieces in sector
     *
     * @param i position of piece
     * @return piece object
     */
    public BoardPiece getPiece(int i) {
        return boardPieces.get(i);
    }

    /**
     * Return the number of boardPieces in the sector
     *
     * @return integer value of numbers in sector
     */
    public int size() {
        return boardPieces.size();
    }
}